﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AncientLand.Common;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers
{
    public class NewsController : Controller
    {
        private readonly INewsBiz _newService;

        public NewsController(INewsBiz newsService)
        {
            this._newService = newsService;
        }

        [ChildActionOnly]
        [OutputCache(Duration = 3600)]
        public PartialViewResult _TopFive()
        {
            var totalRecord = 0;
            var topFiveNews = _newService.GetNewsByPages(out totalRecord,1, 5, "");
            return PartialView("_TopFive", topFiveNews);
        }

        public ActionResult List(int? page=1)
        {

            var size =Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["pageSize"]);

            var totalRecord = 0;
            var news = _newService.GetNewsByPages(out totalRecord,pageIndex:page.GetValueOrDefault(),pageSize:size,orderFiled:"");

            var item = new PageItem()
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages= (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
                               ControllerName = "News",
                               ActionName = "List"
                           };

            var viewModels = new Pager<News>() {DataSource = news, Item = item};

            return View(viewModels);
        }

        public ActionResult Detail(int? id = 1)
        {
            var newsItem = _newService.GetById(id.GetValueOrDefault());
            newsItem.ReadTimes++;
            _newService.Update(newsItem);

            return View(newsItem);
        }

        //[HttpPost]
        public JsonResult SetReadTimes(int? id = 1,int? readTime=1)
        {
            var newsItem = _newService.SetReadTimes(id.GetValueOrDefault(),readTime.GetValueOrDefault());
            //var json = new JsonResult() { Data = SerializationHelper.SerializeToJson<Boolean>(newsItem), JsonRequestBehavior = JsonRequestBehavior.AllowGet };

            return Json(newsItem,JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Edit(int? id = 1)
        {
            var newsItem = _newService.GetById(id.GetValueOrDefault());
            return View(newsItem);
        }

        [HttpPost]
        [ValidateInput(false)]
        public RedirectToRouteResult Edit(News news)
        {
            if(ModelState.IsValid)
            {
                news.CreatedTime = DateTime.Now;
                _newService.Update(news);
            }
            return RedirectToAction("Detail",new{id=news.Id});
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(News news)
        {
            if(ModelState.IsValid)
            {
                news.CreatedTime = DateTime.Now;
                news.ReadTimes = 1;
                _newService.Create(news);
            }
            return View();
        }

    }
}

